<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/16
 * Time: 17:45
 */

namespace app\meizhuangapi\controller;

use app\common\library\MysicLog;
use app\common\model\divide\Receive;
use app\common\model\divide\Service;
use org\RabbitMQ;
use think\Controller;
use think\Db;
use think\Exception;

class Test extends Controller
{
    protected $log;
    public function __construct()
    {
        parent::__construct();
        $this->log = MysicLog::instance();
    }

    public function connTest()
    {
//        $args = $this->request->server()['argv'][2];
        $id = Db::name('test')->where('id', 1)->field('admin_id')->find();
        echo $id['admin_id'],PHP_EOL;
        $s = true;
        sleep(5);
        while(true) {
            $nid = 0;
            try{
                $nid = Db::name('test')->insertGetId(['admin_id' => 44]);
            }catch(\Exception $e) {
                echo 'h',PHP_EOL;
                $s = false;
            }
            if(!$s) {
                try {
                    Db::connect();
                }catch(\Exception $e) {
                    print_r([$e->getMessage(),$e->getFile(),$e->getLine()]);
                }
            }
            echo $nid,PHP_EOL;
            sleep(1);
        }
    }

    public function produce()
    {
        $orderId = $this->request->get('order_id');
        $mq = new RabbitMQ();
        $data=['orderId'=> $orderId,'time'=>time()];
        $this->log->info($data,'mq 消息');
        $mq->send($data,'userListData','userListDataLog','userListDataLog');
    }

    public function dbsave()
    {
        $model = new Receive();
        $receives = $model->where(['wxapp_id' => 2, 'type' => 1, 'periods_num' => 1000])
            ->field('receives_id,type,order_no, times,balance,wxapp_id,payment,periods_num,user_id,account,top_limit,transaction_id')
            ->order('create_time desc')
            ->select()->toArray();
       $balance = 30;
       $upperLimit = 12345678;
       $bonus = 21;
        $model->user_id = 1;
        $model->account = 12;
        $model->order_no = 123;
        $model->wxapp_id = 1234;
        $model->payment = 12345;
        $model->periods_num = 123456;
        $model->transaction_id = 1234567;
        $model->top_limit = 12345678;
        $model->type = 0;
        $model->balance =  $balance > 0 ? $balance : $upperLimit;
//        $model->setInc('total_abonus', $bonus);
        $model->times = $balance > 0 ? 1 : 0;
        $model->create_time = \time();
        $model->update_time = \time();
        $model->isUpdate(false)->save();
       
    }

    public function updateall()
    {
        $status = $this->request->get('status');
        $model = new Service();
        $orders = $model->where('order_no', '2020102357575598')->select();
        $updateList = [];
        foreach($orders as $order) {
            $updateList[] = [
                'id' => $order->id,
                'status' => $status,
                'fail_reason' => '分账成功'
            ];
        }
        $model->saveAll($updateList);
    }
}